import requests
import execjs
import time


def resp(result):
    # 请求头
    headers = {
        'accept': '*/*',
        'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'cache-control': 'no-cache',
        'content-type': 'application/x-www-form-urlencoded',
        # 'cookie': 'NMTID=00O2hWZu8ekSAlG9UBxkkz-3k-tJuwAAAGTj6rAig; JSESSIONID-WYYY=q5286EHyj13WvKOs9%5CeG%2FxwEz3wE267hYzXGC5vgi0z8D0uNQJoCYUh1KDtoCBJ1pImoc8MybhfUyuIrFrsMho%2FRhHJ5HxhoBAkrVire3pZEZKyb88vDqmchV2Ce7UAtN6GmKUBalYwQtx7%2BYiciPlB7Nqy8ToSBfTbdTEgOS4yFfMS%5C%3A1733283953507; _iuqxldmzr_=32; WEVNSM=1.0.0; WNMCID=bwknkw.1733282155167.01.0; WM_NI=if%2BRUo5N%2BYIQQ8vFqkLR2IUrLFE4rSbOAPeVlwzUalx4zWOqeuxpVq23FW3nFnZAWMX0t4NpjwfT2Y%2Bf%2F3vC%2BNehoLlFfY3tAy9rS50ezLBtf%2FZ4JYPho%2FVfDcUVK13LR1A%3D; WM_NIKE=9ca17ae2e6ffcda170e2e6eedac268968aa08ad045a5e78ba6d45f839a8a82d759fb88f99be272acf19e8ce92af0fea7c3b92aabbae5b3d865f792e5bacc5b93e8baa7f461908efeaad87a909a8bd5f159a2918fb1e85eb0a9b6b6e468bcecb6b8ce7ba7abb891d57bf2ed8ab8b26686b296a2d6479cf0bcd5cd7caaa9aab5b153e9bae1d4f549e992ab89ee48b09aaabab28083a7f793d23ff4bbb6adc180a6f196d5c95394b8a1d0d56e88bd9bb7ca3bf1949ed2c437e2a3; WM_TID=aCmwJWLHFWBEVVQBRFKGC8DqnMihuRhn; sDeviceId=YD-OwHUQ2K0zqpBU0ERFBLSSsH%2BmZyyQmSn; ntes_utid=tid._.NzHwBKxgspBAVhRQRBfCT8X6mN3zU3H1._.0',
        'origin': 'https://music.163.com',
        'pragma': 'no-cache',
        'priority': 'u=1, i',
        'referer': 'https://music.163.com/playlist?id=2139324915',
        'sec-ch-ua': '"Microsoft Edge";v="131", "Chromium";v="131", "Not_A Brand";v="24"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
        'sec-fetch-dest': 'empty',
        'sec-fetch-mode': 'cors',
        'sec-fetch-site': 'same-origin',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0',
    }
    # # 表单数据
    data = {
        'params': result['encText'],
        'encSecKey': result['encSecKey'],
    }
    response = requests.post(
        'https://music.163.com/weapi/comment/resource/comments/get',
        headers=headers,
        data=data,
    )
    return response.json()


def get_cursor(cursor, page):
    js_file = open('Test.js', encoding='utf-8').read()
    js_code = execjs.compile(js_file)
    pageNo = page
    result = js_code.call("getData", pageNo, cursor)
    return result


cursor = str(int(time.time() * 1000))
for index in range(1, 21):
    result = get_cursor(cursor, index)
    data = resp(result)
    cursor = data['data']['cursor']
    comments = data['data']['comments']
    for it in comments:
        content = it['content'].strip()
        user = it['user']['nickname']
        print(f"{user} 评论：{content}")
        print("-------------------")

    print(f"第{index}页")
    time.sleep(1)
